/*
 * @Descripttion: 
 * @Version: 
 * @Author: gaohj
 * @Date: 2023-02-11 08:56:08
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2023-02-11 09:06:03
 */
// 运动函数 
// ele 元素 
// options 对象 里面有多个属性 {left: 100, top: 200, opacity: 0.5}
// cb 回调函数 
function move(ele, options, cb = () => {}) {
  let count = 0  
  for (let k in options) {
    count++
    const type = k
    let target = options[k]
    if (type === 'opacity') target *= 100 ;
    const timer = setInterval(() => {
      // 1. 获取当前值
      let current
      if (type === 'opacity') {
        current = window.getComputedStyle(ele)[type] * 100
      } else {
        current = parseInt(window.getComputedStyle(ele)[type])
      }
      // 2. 计算本次距离
      let duration = (target - current) / 10
      duration = duration > 0 ? Math.ceil(duration) : Math.floor(duration)
      // 3. 判断 赋值 还是 停止
      if (current === target) {
        clearInterval(timer)
        count--
        if (count === 0) cb()
      } else {
        if (type === 'opacity') {
          ele.style[type] = (current + duration) / 100
        } else {
          ele.style[type] = current + duration + 'px'
        }
      }
    }, 20)
  }
}
